package Demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class Mythreadpool{
    private BlockingQueue<Runnable> blockingQueue=null;

    public Mythreadpool(int n,int capacity){
        blockingQueue =new ArrayBlockingQueue<>(capacity);
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                while(true){
                    try {
                        Runnable task=blockingQueue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        blockingQueue.put(task);
    }
}

public class Main1 {
    public static void main(String[] args) throws InterruptedException {
        Mythreadpool mythreadpool=new Mythreadpool(5,100);
        for (int i = 0; i < 200; i++) {
            int n=i;
            mythreadpool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+" "+n);
                }
            });
        }
    }
}
